package model

import (
	"gorm.io/gorm"
	"manuscript-service/manurpc/manurpc"
)

type Manu struct {
	gorm.Model
	Title string `gorm:"type:varchar(30)"`
	Looks int    `gorm:"type:int"`
	Cid   int    `gorm:"type:int"`
}

func (m *Manu) ManuAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}
func (m *Manu) FindManuByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(m).Error
}
func ManuShow(db *gorm.DB, id int, list []*manurpc.ManuList) []*manurpc.ManuList {
	db.Debug().Table("manus").Select(
		"manus.id as manu_id",
		"manus.title",
		"manus.looks",
		"classes.class_name",
	).Joins("LEFT JOIN classes ON manus.cid = classes.id").Where("manus.id=?", id).Find(&list)
	return list
}

type Class struct {
	gorm.Model
	ClassName string `gorm:"type:varchar(30)"`
}

func (c *Class) FindClassByName(db *gorm.DB, name string) error {
	return db.Debug().Where("class_name", name).FirstOrCreate(c).Error
}
